package com.bulemobi.view;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.view.Window;
import android.widget.Toast;
import com.google.gson.Gson;
import com.bulemobi.net.tsz.afinal.FinalActivity;
import com.jsyh.hcb.R;

/**
 * @author Administrator
 * 
 */
public abstract class BaseActivity extends FinalActivity {
	public static final String TAG = BaseActivity.class.getSimpleName();
	protected Context context;

	protected Handler mHandler = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		context = this;
	}

	protected void startActivity(Class<?> cls) {
		super.startActivity(new Intent(this, cls));
	}

	private static Gson gson;

	public static Gson getGson() {
		if (gson == null)
			gson = new Gson();
		return gson;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		closeProgressDialog();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	/**
	 * 通过类名启动Activity
	 * 
	 * @param pClass
	 */
	protected void openActivity(Class<?> pClass) {
		openActivity(pClass, null);
	}

	protected void openActivityIntent(Class<?> pClass, Intent intent) {
		intent.setClass(this, pClass);
		startActivity(intent);
	}

	/**
	 * 通过类名启动Activity，并且含有Bundle数据
	 * 
	 * @param pClass
	 * @param pBundle
	 */
	protected void openActivity(Class<?> pClass, Bundle pBundle) {
		Intent intent = new Intent(this, pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}

	/**
	 * 通过Action启动Activity
	 * 
	 * @param pAction
	 */
	protected void openActivity(String pAction) {
		openActivity(pAction, null);
	}

	/**
	 * 通过Action启动Activity，并且含有Bundle数据
	 * 
	 * @param pAction
	 * @param pBundle
	 */
	protected void openActivity(String pAction, Bundle pBundle) {
		Intent intent = new Intent(pAction);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}

	/**
	 * 返回值启动Activity
	 * 
	 */
	protected void openActivityForResult(Class<?> pClass, int requestCode) {
		Intent intent = new Intent(this, pClass);
		startActivityForResult(intent, requestCode);
	}

	/**
	 * 返回值启动Activity
	 * 
	 */
	protected void openActivityForResult(Class<?> pClass, int requestCode,
			Intent intent) {
		intent.setClass(this, pClass);
		startActivityForResult(intent, requestCode);
	}

	protected void DisPlay(String content) {
		Toast.makeText(this, content, 1).show();
	}

	public ProgressDialog progressDialog = null;

	/** 加载进度条 */
	public void showProgressDialog() {

		if (progressDialog != null) {
			progressDialog.cancel();
		}
		progressDialog = new ProgressDialog(this);
		Drawable drawable = getResources().getDrawable(
				R.drawable.loading_animation);
		progressDialog.setIndeterminateDrawable(drawable);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		progressDialog.setMessage(getString(R.string.dialog_msg));
		progressDialog.show();
	}

	public void showProgressDialog(String s) {

		if (progressDialog != null) {
			progressDialog.cancel();
		}
		progressDialog = new ProgressDialog(this);
		Drawable drawable = getResources().getDrawable(
				R.drawable.loading_animation);
		progressDialog.setIndeterminateDrawable(drawable);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		progressDialog.setMessage(s);
		progressDialog.show();
	}

	/** 加载进度条 */
	public void showProgressDialogCancelable() {

		if (progressDialog != null) {
			progressDialog.cancel();
		}
		progressDialog = new ProgressDialog(this);
		Drawable drawable = getResources().getDrawable(
				R.drawable.loading_animation);
		progressDialog.setIndeterminateDrawable(drawable);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.setMessage(getString(R.string.dialog_msg));
		progressDialog.show();
	}

	public void showProgressDialogCancelable(String s) {

		if (progressDialog != null) {
			progressDialog.cancel();
		}
		progressDialog = new ProgressDialog(this);
		Drawable drawable = getResources().getDrawable(
				R.drawable.loading_animation);
		progressDialog.setIndeterminateDrawable(drawable);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.setMessage(s);
		progressDialog.show();
	}

	public void closeProgressDialog() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	/**
	 * 显示toast
	 */
	public void showToastShort(String msg) {
		showToast(this, msg, Toast.LENGTH_SHORT);
	}

	/**
	 * 显示toast
	 */
	public void showToastLong(String msg) {
		showToast(this, msg, Toast.LENGTH_LONG);

	}

	public static void showToast(Context contex, String msg, int time) {
		Toast.makeText(contex, msg, time).show();
	}

	/**
	 * 拼接字符串
	 */
	public static String getString(String... strings) {
		if (strings.length <= 0)
			return null;
		StringBuilder sb = new StringBuilder();
		for (String s : strings) {
			sb.append(s);
		}
		return sb.toString();
	}

	/**
	 * 根据unix时间戳获取时间
	 */
	public static String getDateFromUnix(long time) {
		return getDateFromUnix(time, "yyyy-MM-dd");
	}

	public static String getDateFromUnix(long time, String geshi) {
		return new SimpleDateFormat(geshi).format(new Date(time * 1000));
	}

    public static String getNowDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }
	public static long getDateTimeFromUnix(long time) {
		return new Date(time * 1000).getTime();
	}

    public static String getIMEI(Context context){
         return  ((TelephonyManager)context.getSystemService(TELEPHONY_SERVICE))
                .getDeviceId();
    }
}
